﻿using System;
using System.ServiceModel;
using __ts.Core.Log;

namespace __ts.Core.Channel
{
    /// <summary>
    /// 
    /// </summary>
    public class CCUChannel
    {
        public static void Create<I>(string ip, string port, Action<I> action, bool rotuer)
        {
            NetTcpBinding bind = new NetTcpBinding();
            bind.MaxReceivedMessageSize = 50971520;
            bind.ReceiveTimeout = new TimeSpan(0, 10, 0);
            bind.SendTimeout = new TimeSpan(0, 10, 0);
            bind.Security.Mode = SecurityMode.None;
            bind.ReaderQuotas.MaxArrayLength = 2147483647;
            bind.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows;
            bind.Security.Message.ClientCredentialType = MessageCredentialType.Windows;

            //CCUChannelExtra.GetEntity = null;

            EndpointAddress ep = null;
            if (rotuer)
                ep = new EndpointAddress(string.Format("net.tcp://{0}:{1}/{2}", ip, port, "Router_1_0_0"));
            else
                ep = new EndpointAddress(string.Format("net.tcp://{0}:{1}/{2}", ip, port, typeof(I).Namespace));

            using (ChannelFactory<I> channelFactory = new ChannelFactory<I>(bind, ep))
            {
                //channelFactory.Endpoint.Behaviors.Add(new CCU.Core.Channel.CCUBehavior());
                I proxy = (I)channelFactory.CreateChannel();
                try
                {
                    action(proxy);
                    ((ICommunicationObject)proxy).Close();
                }
                catch (CommunicationException ex)
                {
                    //CCUChannelExtra.GetEntity.output = ex.Message;
                    Logger.Write(ex);
                    ((ICommunicationObject)proxy).Abort();
                }
                catch (TimeoutException ex)
                {
                    //CCUChannelExtra.GetEntity.output = ex.Message;
                    Logger.Write(ex);
                    ((ICommunicationObject)proxy).Abort();
                }
                catch (Exception ex)
                {
                    //CCUChannelExtra.GetEntity.output = ex.Message;
                    Logger.Write(ex);
                    ((ICommunicationObject)proxy).Abort();
                }
            }
        }

        public static void Create<I>(string ip, string port, Action<I> action)
        {
            NetTcpBinding bind = new NetTcpBinding();
            bind.MaxReceivedMessageSize = 50971520;
            bind.ReceiveTimeout = new TimeSpan(0, 10, 0);
            bind.SendTimeout = new TimeSpan(0, 10, 0);
            bind.Security.Mode = SecurityMode.None;
            bind.ReaderQuotas.MaxArrayLength = 2147483647;
            bind.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows;
            bind.Security.Message.ClientCredentialType = MessageCredentialType.Windows;

            //CCUChannelExtra.GetEntity = null;

            EndpointAddress ep = new EndpointAddress(string.Format("net.tcp://{0}:{1}/{2}", ip, port, typeof(I).Namespace));

            using (ChannelFactory<I> channelFactory = new ChannelFactory<I>(bind, ep))
            {
                //channelFactory.Endpoint.Behaviors.Add(new CCU.Core.Channel.CCUBehavior());
                I proxy = (I)channelFactory.CreateChannel();
                try
                {
                    action(proxy);
                    ((ICommunicationObject)proxy).Close();
                }
                catch (CommunicationException ex)
                {
                    //CCUChannelExtra.GetEntity.output = ex.Message;
                    Logger.Write(ex);
                    ((ICommunicationObject)proxy).Abort();
                }
                catch (TimeoutException ex)
                {
                    //CCUChannelExtra.GetEntity.output = ex.Message;
                    Logger.Write(ex);
                    ((ICommunicationObject)proxy).Abort();
                }
                catch (Exception ex)
                {
                    //CCUChannelExtra.GetEntity.output = ex.Message;
                    Logger.Write(ex);
                    ((ICommunicationObject)proxy).Abort();
                }
            }
        }
    }
}
